<html>
<head><meta charset="utf-8"><title>Never type in tuple · t-lang/project-never-type · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/index.html">t-lang/project-never-type</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html">Never type in tuple</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="237174326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237174326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237174326">(May 03 2021 at 15:01)</a>:</h4>
<p>Hi all.</p>
<p>I post here in order to confirm whether rustc is buggy when dealing the code segment below:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="k">return</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="mi">1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// Error: expected `!`, found integer</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>since rustc thinks the following code is okay:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="mi">1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Essentially I don't think there are any differences between the two code segments.</p>
<p>It may cause problems similar to this one <a href="https://github.com/rust-lang/rust/issues/66173#issuecomment-574892360">https://github.com/rust-lang/rust/issues/66173#issuecomment-574892360</a>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">empty_func</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">unconstrained_return</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span>: <span class="nc">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">&amp;</span><span class="n">empty_func</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">f</span><span class="p">())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">unconstrained_return</span>::<span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="c1">// Here the type of `x` is `((), !)`.</span>
<span class="w">        </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">((),</span><span class="w"> </span><span class="k">return</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Besides tuples, a few other expressions suffer from the same problem:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="k">box</span><span class="w"> </span><span class="k">return</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot add `{integer}` to `Box&lt;!&gt;`</span>
<span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="k">return</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot apply unary operator `-` to type `!`</span>
<span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!</span><span class="k">return</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot apply unary operator `!` to type `!`</span>
<span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="k">return</span><span class="p">;</span><span class="w"> </span><span class="c1">// type `!` cannot be dereferenced</span>
<span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="k">return</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// cannot add `{integer}` to `&amp;!`</span>
</code></pre></div>
<p>I think <code>!</code> shouldn't appear in above error messages since I don't enable the feature gate <code>never_type_fallback</code>.</p>
<p>I am working on it and if it is considered as a bug, I will open a PR that fixes it.</p>



<a name="237292172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237292172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237292172">(May 04 2021 at 09:15)</a>:</h4>
<p>According to <a href="https://godbolt.org/">https://godbolt.org/</a>, it seems that we have the same behavior since 1.24.0. And before 1.24.0, coercing anything to <code>!</code> is allowed, which is a different issue.<br>
The key question is that I'm not sure whether or not the behavior here is expected. Although nobody will actually write code like this, I think it deserves a small fix if the behavior is unexpected. Does it make sense?</p>



<a name="237303480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237303480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237303480">(May 04 2021 at 10:54)</a>:</h4>
<p>Hmm. It sort of depends on the fix, I'm not sure I'm inclined to break compiling code.</p>



<a name="237303491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237303491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237303491">(May 04 2021 at 10:54)</a>:</h4>
<p>Cc <span class="user-mention" data-user-id="116009">@nikomatsakis</span></p>



<a name="237303540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237303540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237303540">(May 04 2021 at 10:54)</a>:</h4>
<p>We are working on a path towards landing never type stabilization more broadly, FWIW.</p>



<a name="237385646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237385646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237385646">(May 04 2021 at 19:36)</a>:</h4>
<p>hmm, I'm not entirely sure how that comes about</p>



<a name="237385918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237385918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237385918">(May 04 2021 at 19:38)</a>:</h4>
<p>I guess what happens is that <code>return</code> has type <code>!</code> and we never introduce a type variable when it is part of a tuple?</p>



<a name="237433732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237433732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237433732">(May 05 2021 at 03:00)</a>:</h4>
<blockquote>
<p>We are working on a path towards landing never type stabilization more broadly, FWIW.</p>
</blockquote>
<p>Yeah, I'm aware about it. I don't think the never type stabilization is helpful towards this issue. I believe we are still unable to compile the first code segment even if we have the stabilization done.</p>



<a name="237433765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237433765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237433765">(May 05 2021 at 03:00)</a>:</h4>
<blockquote>
<p>I guess what happens is that <code>return</code> has type <code>!</code> and we never introduce a type variable when it is part of a tuple?</p>
</blockquote>
<p>Yes, exactly. In most cases the never type becomes a diverging type variable only when <a href="https://github.com/rust-lang/rust/blob/ae8b84bf04cddda2379b36c45a575132e6a44fb0/compiler/rustc_typeck/src/check/coercion.rs#L169">coercion occurs</a>. But in the cases that I mentioned before, we <a href="https://github.com/rust-lang/rust/blob/ae8b84bf04cddda2379b36c45a575132e6a44fb0/compiler/rustc_typeck/src/check/expr.rs#L1104">won't coerce</a> the never type with any other types.</p>



<a name="237433913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237433913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237433913">(May 05 2021 at 03:02)</a>:</h4>
<blockquote>
<p>Hmm. It sort of depends on the fix, I'm not sure I'm inclined to break compiling code.</p>
</blockquote>
<p>Actually if we just want to fix the problem about tuples, it should be very easy and it won't break anything almost certainly.</p>
<p>But these days I am considering whether I can fix the problem in a more general way. For example, maybe we should convert the never type to the diverging type just at the end of the function <code>check_expr_with_expectation</code>, like this <a href="https://github.com/lrh2000/rust/commit/fa1c9ebe2af750d92ed73669c34cfdfd4862fe1a">draft commit</a>. But interestingly and very unexpectedly, it changes error messages emitted by the region check, although they do not seem to be real regressions. Maybe later I will post some details here.</p>



<a name="237434537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237434537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237434537">(May 05 2021 at 03:12)</a>:</h4>
<p>(Since I'm relatively new to the rustc compiler community, it is likely that I did something wrong. Feel free to point them out and thanks a lot for any feedback.)</p>



<a name="237496825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237496825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237496825">(May 05 2021 at 13:43)</a>:</h4>
<p>Yeah, I'm debating about it. The question is basically whether to ever <em>assign</em> something the 'never type' at all</p>



<a name="237496845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237496845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237496845">(May 05 2021 at 13:43)</a>:</h4>
<p>Or whether it always begins as a diverging type variable</p>



<a name="237497011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237497011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237497011">(May 05 2021 at 13:44)</a>:</h4>
<p>this code:</p>



<a name="237497032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237497032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237497032">(May 05 2021 at 13:44)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">unconstrained_return</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span>: <span class="nc">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">&amp;</span><span class="n">empty_func</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">f</span><span class="p">())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="237497043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237497043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237497043">(May 05 2021 at 13:44)</a>:</h4>
<p>is really a fairly broken pattern</p>



<a name="237497058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237497058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237497058">(May 05 2021 at 13:44)</a>:</h4>
<p>I wonder how widespread it is</p>



<a name="237498103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498103">(May 05 2021 at 13:52)</a>:</h4>
<blockquote>
<p>is really a fairly broken pattern</p>
</blockquote>
<p>Yeah, I think so. But isn't the code an example that you gave in <a href="https://github.com/rust-lang/rust/issues/66173#issuecomment-574892360">https://github.com/rust-lang/rust/issues/66173#issuecomment-574892360</a> ?</p>



<a name="237498194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498194">(May 05 2021 at 13:52)</a>:</h4>
<p>Actually I am very curious about how the never type fallback can cause problems <strong>without any unsafe code</strong>. Even more, is there a regression in real code segments (used in production)?</p>



<a name="237498250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498250">(May 05 2021 at 13:53)</a>:</h4>
<p><span class="user-mention" data-user-id="407862">@lrh2000</span> it is</p>



<a name="237498274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498274">(May 05 2021 at 13:53)</a>:</h4>
<p>I'm only just now realizing that we've been approaching the "lint" from the wrong side</p>



<a name="237498294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498294">(May 05 2021 at 13:53)</a>:</h4>
<p>that is, we were approaching is "lint for when this code winds up falling back to never"</p>



<a name="237498318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498318">(May 05 2021 at 13:53)</a>:</h4>
<p>but we should perhaps have been approaching it from "lint for code that can produce values of any type"</p>



<a name="237498395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498395">(May 05 2021 at 13:54)</a>:</h4>
<p>or 'potentially uninhabited' types</p>



<a name="237498433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498433">(May 05 2021 at 13:54)</a>:</h4>
<p>and encouraging the use of <code>MaybeUnit</code> or other such constructs</p>



<a name="237498495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498495">(May 05 2021 at 13:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="407862">lrh2000</span> <a href="#narrow/stream/259160-t-lang.2Fproject-never-type/topic/Never.20type.20in.20tuple/near/237498194">said</a>:</p>
<blockquote>
<p>Actually I am very curious about how the never type fallback can cause problems <strong>without any unsafe code</strong>. Even more, is there a regression in real code segments (used in production)?</p>
</blockquote>
<p>define problems :)</p>



<a name="237498521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498521">(May 05 2021 at 13:55)</a>:</h4>
<p>I don't believe it causes <em>unsoundness</em></p>



<a name="237498598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498598">(May 05 2021 at 13:55)</a>:</h4>
<p>well</p>



<a name="237498606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237498606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237498606">(May 05 2021 at 13:55)</a>:</h4>
<p>it depends on one's definition, I  guess</p>



<a name="237499515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237499515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237499515">(May 05 2021 at 14:01)</a>:</h4>
<p>I believe producing value of any types must require unsafe code, and fairly broken unsafe code. In my opinion, it should be very obvious and even needn't a lint to detect.</p>



<a name="237499585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237499585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237499585">(May 05 2021 at 14:01)</a>:</h4>
<p>Is there any good examples to show it is useful?</p>



<a name="237499993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237499993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237499993">(May 05 2021 at 14:04)</a>:</h4>
<p>I think the common thing is when you "know" the types you expect to instantiate with but for whatever reason don't want a trait or so</p>



<a name="237500058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237500058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237500058">(May 05 2021 at 14:04)</a>:</h4>
<p>not sure that's <em>good</em> though</p>



<a name="237500725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237500725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237500725">(May 05 2021 at 14:08)</a>:</h4>
<p>How can you produce a value of <em>any type</em> in such a way? I think it must mean that we use unsafe code to operate directly the memory on a generic parameter. Then the code is just totally broken and it won't be used in any real scenarios.</p>



<a name="237516160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237516160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237516160">(May 05 2021 at 15:31)</a>:</h4>
<blockquote>
<p>Or whether it always begins as a diverging type variable</p>
</blockquote>
<p>In favor of consistency, I generally prefer this. Here is another <a href="https://github.com/lrh2000/rust/commit/82dadf0eb66b5ed36844361ec4d3dbf4143b4f3a">draft commit</a> but with fixed regression tests.<br>
Maybe you can have a look at it? I'd like to file a PR in one or two days if I don't hear any negative feedback from here.</p>



<a name="237525027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237525027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237525027">(May 05 2021 at 16:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="407862">lrh2000</span> <a href="#narrow/stream/259160-t-lang.2Fproject-never-type/topic/Never.20type.20in.20tuple/near/237499515">said</a>:</p>
<blockquote>
<p>I believe producing value of any types must require unsafe code, and fairly broken unsafe code. In my opinion, it should be very obvious and even needn't a lint to detect.</p>
</blockquote>
<p>Well, we know from experience it happens :)</p>



<a name="237525085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237525085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237525085">(May 05 2021 at 16:27)</a>:</h4>
<p>It's not always so obvious, sadly; the case in question was the objc crate, where the type came from user annotations</p>



<a name="237628795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237628795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237628795">(May 06 2021 at 09:19)</a>:</h4>
<p>Another idea. How about just always beginning from a diverging type, but performing the never type fallback conditionally in the <code>structurally_resolved_type</code> if the diverging type is not unified with other type variables (non-diverging type variables)?</p>



<a name="237628806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237628806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237628806">(May 06 2021 at 09:19)</a>:</h4>
<p>Now we have this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="o">!</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="n">a</span><span class="p">.</span><span class="n">foo</span><span class="p">();</span><span class="w"> </span><span class="c1">//~ ERROR no method named `foo` found for type `!` in the current scope</span>
<span class="p">{</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">}.</span><span class="n">foo</span><span class="p">();</span><span class="w"> </span><span class="c1">//~ ERROR type annotations needed</span>
<span class="fm">panic!</span><span class="p">().</span><span class="n">foo</span><span class="p">();</span><span class="w"> </span><span class="c1">//~ ERROR type annotations needed</span>
</code></pre></div>
<p>The last two statements failed because <code>structurally_resolved_type</code> cannot resolve the diverging type. We can coerce the diverging type to <code>()</code> or <code>!</code> <em>if we know this won't affect other code</em>.</p>



<a name="237628812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237628812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237628812">(May 06 2021 at 09:19)</a>:</h4>
<p>I am trying to check whether the variable is unified with other non-diverging variables in the unification table. But maybe I need to ignore a lot of auxiliary type variables according to <code>TypeVariableOriginKind</code>.</p>
<p>I am fairly unsure about whether I am on the right way. Do you think it works?</p>



<a name="237644510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/259160-t-lang/project-never-type/topic/Never%20type%20in%20tuple/near/237644510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/259160-t-lang/project-never-type/topic/Never.20type.20in.20tuple.html#237644510">(May 06 2021 at 11:47)</a>:</h4>
<p>A proof-of-concept seems to show it works well.</p>
<div class="codehilite"><pre><span></span><code>error[E0282]: type annotations needed
 --&gt; y.rs:3:5
  |
2 |     let a = return;
  |         - consider giving `a` a type
3 |     { if true { a } else { return } }.foo();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
  |
  = note: type must be known at this point
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0599]: no method named `foo` found for unit type `!` in the current scope
 --&gt; y.rs:2:44
  |
2 |     { if true { return } else { return } }.foo();
  |                                            ^^^ method not found in `!`
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0599]: no method named `foo` found for unit type `!` in the current scope
 --&gt; y.rs:4:39
  |
4 |     { if true { a } else { return } }.foo();
  |                                       ^^^ method not found in `!`
</code></pre></div>
<p>where we have <code>let a: ! = return</code> in the last case.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>